package com.libui.thirdpartsdk.wechat

import com.libbase.Weak
import com.libui.thirdpartsdk.SDKInterface
import com.libui.thirdpartsdk.SDKManager
import com.libui.thirdpartsdk.share.ShareCompletion
import com.libui.thirdpartsdk.share.ShareInterface
import com.libui.thirdpartsdk.share.models.ShareImageObject
import com.libui.thirdpartsdk.share.models.ShareObjectType
import com.libui.thirdpartsdk.share.models.ShareUrlObject
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXImageObject
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject
import com.tencent.mm.opensdk.openapi.IWXAPI

open class WechatShare(wxApi: IWXAPI?) : ShareInterface {
    companion object {
        val builder = object : ShareInterface.Builder {
            override val type: String = "wechat"

            override val title: String = "微信好友"

            override val icon: String = "wechat.png"

            override val selectedIcon: String? = null

            override fun build(sdk: SDKInterface): ShareInterface {
                return WechatShare((sdk as? WechatSDK)?.wxApi)
            }
        }
    }

    override val shareObjectTypes: List<ShareObjectType> = listOf(
        ShareObjectType.URL,
        ShareObjectType.IMAGE,
    )

    override val type: String
        get() = builder.type

    override val title: String
        get() = builder.title

    override val icon: String?
        get() = builder.icon

    override val selectedIcon: String?
        get() = builder.selectedIcon

    override var isSelected: Boolean = false

    private var wxApi: IWXAPI? by Weak { wxApi }

    open val scene: Int = SendMessageToWX.Req.WXSceneSession

    override fun shareUrl(shareObject: ShareUrlObject, completion: ShareCompletion?) {
        val webPage = WXWebpageObject()
        webPage.webpageUrl = shareObject.url

        val message = WXMediaMessage(webPage)
        message.title = shareObject.title
        message.description = shareObject.description
        message.thumbData = shareObject.thumbImage ?: SDKManager.instance.defaultThumbImage

        send(message, "webpage", completion)
    }

    override fun shareImage(shareObject: ShareImageObject, completion: ShareCompletion?) {
        val imageObject = WXImageObject()
        imageObject.imageData = shareObject.imageData

        val message = WXMediaMessage(imageObject)

        send(message, buildTransaction("img"), completion)
    }

    private fun buildTransaction(type: String?): String {
        return if (type == null) System.currentTimeMillis().toString()
        else type + System.currentTimeMillis()
    }

    private fun send(message: WXMediaMessage, type: String, completion: ShareCompletion?) {
        val req = SendMessageToWX.Req()
        req.transaction = buildTransaction(type)
        req.scene = scene
        req.message = message

        completion?.let { it(wxApi?.sendReq(req) == true) }
    }
}